package com.dk.service.goods;

import com.alibaba.dubbo.config.annotation.Service;
import com.dk.mapper.goods.TbGoodsDescMapper;
import com.dk.mapper.goods.TbGoodsMapper;
import com.dk.mapper.item.TbItemMapper;
import com.dk.pojo.goods.TbGoods;
import com.dk.pojo.goods.TbGoodsDesc;
import com.dk.pojo.item.TbItem;
import com.dk.pojo.item.TbItemExample;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Component
public class GoodServiceImpl implements GoodsService {
    @Resource
    private TbGoodsMapper goodsMapper;

    @Resource
    private TbGoodsDescMapper goodsDescMapper;

    @Resource
    private TbItemMapper itemMapper;

    @Override
    public List<TbItem> listItem(Long goodsId) {
        TbItemExample example = new TbItemExample();
        TbItemExample.Criteria criteria = example.createCriteria();
        criteria.andGoodsIdEqualTo(goodsId);
        return itemMapper.selectByExample(example);
    }

    @Override
    public TbGoodsDesc getGoodsDescById(Long goodsId) {
        return goodsDescMapper.selectByPrimaryKey(goodsId);
    }

    @Override
    public TbGoods getGoodsById(Long goodsId) {
        return goodsMapper.selectByPrimaryKey(goodsId);
    }

    /**
     * 商品审核
     * @param status
     * @param ids
     * @return
     */
    @Override
    public int goodsEach(String status, Long[] ids) {
        for (int i = 0; i < ids.length; i++) {
            TbGoods goods = new TbGoods();
            goods.setId(ids[i]);
            goods.setAuditStatus(status);
            goodsMapper.updateByPrimaryKeySelective(goods);
        }
        return 0;
    }

    @Override
    public List<TbGoods> listGoods() {
        return goodsMapper.selectByExample(null);
    }

    @Transactional
    @Override
    public int saveGoods(TbGoods goods, TbGoodsDesc goodsDesc, List<TbItem> tbItems, List<String> skuStrings) {
        goodsMapper.saveGoodsResult(goods);
        goodsDesc.setGoodsId(goods.getId());
        int j = goodsDescMapper.insertSelective(goodsDesc);
        for (int i = 0;i < tbItems.size();i++) {
            TbItem items = tbItems.get(i);
            items.setCreateTime(new Date());
            items.setUpdateTime(new Date());
            items.setGoodsId(goods.getId());
            items.setImage(goodsDesc.getItemImages());
            items.setSellerId(goods.getSellerId());
            items.setStatus("1");
            items.setSpec(skuStrings.get(i));
            int i1 = itemMapper.insertSelective(items);
        }

        return 0;
    }
}
